﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TournamentOrganizerWPF;

namespace TournamentOrganizerWPF
{
    /// <summary>
    /// Interaction logic for MatchListWindow.xaml
    /// </summary>
    public partial class MatchListWindow : Window
    {
        private int selectedMatchIndex;
        public ObservableCollection<Match> ActiveMatchList { get; set; }
        Tournament tournament;
        Match currentMatch;

        public MatchListWindow(Tournament tournament)
        {
            this.tournament = tournament;
            InitializeComponent();
            this.DataContext = tournament;
            ActiveMatchList = new ObservableCollection<Match>();
            
        }


        private void ReportResult(object sender, RoutedEventArgs e)
        {
            WarningLabel.Content = "";
            currentMatch = tournament.ActiveMatches.ElementAt(selectedMatchIndex);

            currentMatch.ControlPointsPlayer1 = Int32.Parse(Player1CP.Text);
            currentMatch.ControlPointsPlayer2 = Int32.Parse(Player2CP.Text);

            if (WinnerPLayer1CheckBox.IsChecked == false && WinnerPLayer2CheckBox.IsChecked == false)
            {
                WarningLabel.Content = "You must select a winner";
            }

            if (WinnerPLayer1CheckBox.IsChecked == true && WinnerPLayer2CheckBox.IsChecked == false)
	        {
                currentMatch.WinningPlayer = currentMatch.Player1;
            }
            else if (WinnerPLayer1CheckBox.IsChecked == false && WinnerPLayer2CheckBox.IsChecked == true)
            {
                currentMatch.WinningPlayer = currentMatch.Player2;
            }
            
            tournament.ReportResult(currentMatch);
            WinnerPLayer1CheckBox.IsChecked = false;
            WinnerPLayer1CheckBox.IsChecked = false;

            
            ActiveMatches.UpdateLayout();
            WarningLabel.Content = "Match is reported";
        }

        

        private void ActiveMatchesMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (ActiveMatches.SelectedIndex > -1)
            {
                selectedMatchIndex = ActiveMatches.SelectedIndex;

                currentMatch = tournament.ActiveMatches.ElementAt(selectedMatchIndex);

                selectedMatchIndex = ActiveMatches.SelectedIndex;
                SelectedMatch.Content = ActiveMatches.SelectedItem.ToString();

                Player1CP.Text = currentMatch.ControlPointsPlayer1.ToString();
                Player2CP.Text = currentMatch.ControlPointsPlayer2.ToString();
            }                        
        }        
    }
}
